package top.carrotguo.weibo.ui.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import top.carrotguo.weibo.presenter.BasePresenter;
import top.carrotguo.weibo.view.IView;


/**
 * Function: 封装的MVP的BaseFragment
 * Author: Bison
 * Date: 2018/01/05
 * Email: bisonqin@gmail.com
 */

public abstract class MVPBaseFragment<V extends IView, T extends BasePresenter<V>> extends BaseFragment {

    protected T mPresenter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //允许为空，不是所有都要实现MVP模式
        if(createPresenter()!=null) {
            mPresenter = createPresenter();
            mPresenter.attachView((V) this);
        }
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if(mPresenter!=null) {
            mPresenter.detachView();
        }
    }

    protected abstract T createPresenter();
}
